package com.wnhz.work.jy.activity;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.solidfire.gson.Gson;
import com.wnhz.work.jy.BaseActivity;
import com.wnhz.work.jy.MyApplication;
import com.wnhz.work.jy.R;
import com.wnhz.work.jy.adapter.PerformAdapter;
import com.wnhz.work.jy.bean.PerformBean;
import com.wnhz.work.jy.utils.ConfigUtil;
import com.wnhz.work.jy.utils.xutils3.MyCallBack;
import com.wnhz.work.jy.utils.xutils3.XUtil;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.xutils.view.annotation.ViewInject;
import org.xutils.x;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * ---我的业绩
 */
public class MyPerformanceActivity extends BaseActivity implements View.OnClickListener {
    private static final String TAG = "MyPerformanceActivity";
    private TextView title;
    private ListView listView;
    private List<PerformBean> perList = new ArrayList<>();
    private PerformAdapter mAdapter;
    private TextView tv_all_per;

    private String allmoney;

    private String[] phones = {"155****1526", "185****1866", "188****1626", "139****1362", "133****4926", "192****1586"};
    private String[] moneys = {"1235.00", "1285.00", "1985.00", "5214.00", "9854.00", "3224.00"};
    private String[] times = {"10-02 10:36", "10-06 10:36", "10-12 10:38", "11-02 10:36", "11-11 11:11", "12-12 12:36"};
    @ViewInject(R.id.include_occupied)
    private View include_occupied;
    @ViewInject(R.id.blank_img)
    private ImageView blank_img;
    @ViewInject(R.id.blank_tv)
    private TextView blank_tv;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_my_performance);
        x.view().inject(this);
        initView();
        setBlank();
        getData();
    }

    private void setBlank(){
        if (perList.size() == 0){
            blank_tv.setText("单子还在来的路上~");
            blank_img.setImageResource(R.mipmap.ic_wodeyejiweikong);
            include_occupied.setVisibility(View.VISIBLE);
        }else {
            include_occupied.setVisibility(View.GONE);
        }
    }

    private void initView() {
        title = (TextView) findViewById(R.id.view_main_middle_title);
        title.setText("我的业绩");
        findViewById(R.id.btn_main_left).setOnClickListener(this);

        listView = (ListView) findViewById(R.id.lv_performance);
        mAdapter = new PerformAdapter(MyPerformanceActivity.this, perList);
        listView.setAdapter(mAdapter);
        tv_all_per = (TextView) findViewById(R.id.tv_all_per);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_main_left:
                finish();
                break;

            default:
                break;
        }
    }

    private void getData(){
        Map<String,Object> map = new HashMap<>();
        map.put("userId", MyApplication.getInstance().userBean.getUserId());
        showDialog();
        XUtil.Post(ConfigUtil.MYPERFORMANCE,map,new MyCallBack<String>(){
            @Override
            public void onSuccess(String result) {
                super.onSuccess(result);
                Log.e(TAG, "onSuccess: "+result );
                perList.clear();
                try {
                    JSONObject json = new JSONObject(result);
                    allmoney = json.optString("allmoney");
                    JSONArray array = json.optJSONArray("information");
                    if (array != null){
                        for (int i = 0; i < array.length(); i++) {
                            PerformBean bean = new PerformBean();
                            bean = new Gson().fromJson(array.optString(i),PerformBean.class);
                            perList.add(bean);
                        }
                    }
                    tv_all_per.setText(allmoney);
                    mAdapter.notifyDataSetChanged();
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onFinished() {
                super.onFinished();
                setBlank();
                closeDialog();
            }
        });

    }
}
